package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void shoppingCartAdd(ShoppingCartDTO shoppingCartDTO) {
        //先查询购物车表
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.queryShoppingCart(shoppingCart);
        if(list != null && !list.isEmpty()){
            //如果有相同的菜品则只需要数量加1即可
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
        }else {
            //若没有该菜品或套餐
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if(dishId != null){
                DishVO dish = dishMapper.getById(dishId);
                //本次插入为菜品
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else{
                SetmealVO setmeal = setmealMapper.getById(setmealId);
                //本次插入为套餐
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加新实体并且设置初始数量为1
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> shoppingCartList() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list =  shoppingCartMapper.list(userId);
        return list;
    }

    @Override
    public void cleanCartService() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void cleanOneShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //把商品信息分装到实体类中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询商品信息
        List<ShoppingCart> list = shoppingCartMapper.queryShoppingCart(shoppingCart);
        //若数量大于一修改数量
        ShoppingCart shop = list.get(0);
        if(shop.getNumber()>1){
            shop.setNumber(shop.getNumber()-1);
            shoppingCartMapper.updateNumber(shop);
        }else {
            //若数量等于一删除商品
            shoppingCartMapper.deleteOne(shop);
        }
    }
}
